import pytest
from selenium import webdriver
from pluggy import HookspecMarker
from selenium.webdriver.chrome.options import Options
from Case.python_ui.Common.get_sql_object import GetSqlObject
from Case.python_ui.Test_data.common_data import Commondata
from selenium.webdriver.chrome.service import Service

#研发云
import pytest
#from pluggy import HookspecMarker
#from RunResultAgent import TestRunResultAgent, CaseResult
#hookspec = HookspecMarker("pytest")
#agent_obj = TestRunResultAgent()
#is_pytest_multiprocess = True # 并行执行设置成True，无需并行设置成False
import threading

# 修改你的 chromedriver 实际路径
driver_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

def pytest_addoption(parser):
    parser.addoption(
        "--cmdopt", action="store", default="sit", help="my option: sit or uat"
    )


@pytest.fixture(scope="class")
def cmdopt(request):
    return request.config.getoption("--cmdopt")


@pytest.fixture(scope="session", autouse=True)
def get_database_source():
    mysql_source = GetSqlObject.get_instance("sit")
    yield mysql_source


@pytest.fixture(scope="session")
def lock():
    lock = threading.Lock()
    yield lock


@pytest.fixture
def get_customer_driver():
    # 修改浏览器驱动加载策略
    chrome_options = Options()
    chrome_options.page_load_strategy = 'normal'
    chrome_options.add_argument('window-size=1920x1080')
    chrome_options.add_argument('--ignore-certificate-errors')  # 忽略证书错误
    chrome_options.add_argument('--allow-running-insecure-content')  # 允许运行不安全的内容
    #chrome_options.add_argument("--headless")  # 无界面模式下运行chrome driver，Chrome在后台运行
    chrome_options.add_argument('--start-maximized')
    chrome_options.page_load_strategy = 'normal'  # eager: HTML完全加载和解析完毕就开始执行操作。放弃等待图片、样式、子帧的加载
    #chrome_options.add_argument("--incognito")  # 启用无痕模式
    #prefs ={"profile.managed_default_content_settings.images": 2}
    #chrome_options.add_experimental_option("prefs", prefs)
    #chrome_options.add_argument('--blink-settings=imagesEnabled=false')
    prefs = {'profile.default_content_settings.popups': 0,
             'download.default_directory': Commondata.download_file_path}
    chrome_options.add_experimental_option('prefs', prefs)
    #service = Service(executable_path=driver_path)
    #driver = webdriver.Chrome(service=service,options=chrome_options)
    driver = webdriver.Chrome(options=chrome_options)
    driver.implicitly_wait(10)
    driver.maximize_window()
    yield driver
    #driver.quit()




#研发云的
#def pytest_collection_finish(session):
    #agent_obj.init_env(len(session.items))
    #agent_obj.start_suite('test_suite')


#@hookspec()
#def pytest_runtest_protocol(item, nextitem):
    #case_name = item.name
    #agent_obj.start_test(case_name)


@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item):
     #获取钩子方法的调用结果，返回一个result对象
     out = yield
     #从钩子方法的调用结果中获取测试报告
     report = out.get_result()
     if report.when == "call":
        case_name = report.nodeid
        passed = True if report.outcome == "passed" else False
        #agent_obj.end_test(case_result=CaseResult(case_name, passed=passed), is_pytest_multiprocess=is_pytest_multiprocess)


#def pytest_terminal_summary():
    #agent_obj.end_suite(is_pytest_multiprocess=is_pytest_multiprocess)
    #agent_obj.close(is_pytest_multiprocess=is_pytest_multiprocess)